package service.system.impl;

import java.util.ArrayList;
import java.util.List;

import model.security.UsrAuth;
import model.security.UsrRes;
import model.security.UsrRole;
import model.security.UsrRoleAuth;
import model.security.UsrUser;
import model.security.UsrUserRole;

import core.service.BaseService;

import dao.security.UsrAuthDao;
import dao.security.UsrResDao;
import dao.security.UsrRoleAuthDao;
import dao.security.UsrRoleDao;
import dao.security.UsrUserDao;
import dao.security.UsrUserRoleDao;
import exception.Exceptions;

import service.system.SystemListService;
import util.Time;

public class SystemListServiceImpl extends BaseService implements SystemListService {
	private UsrUserDao userDao;
	private UsrRoleDao roleDao;
	private UsrResDao resDao;
	private UsrAuthDao authDao;
	
	private UsrUserRoleDao userRoleDao;
	private UsrRoleAuthDao roleAuthDao;

	@Override
	//-------------user-------------
	public List get_userlist() {
 		return userDao.findModel("useraccount!='admin' and useraccount!='dev'",10);
	}

 	public UsrUser get_user(long userid){
		return userDao.findOne("userid",userid);
	}
 	
 	public void updateUser(UsrUser user,long roleid) throws Exceptions{
 		userDao.update(user);
 		
 		updateUserRole(user.getUserid(),roleid);
 	}

	@Override
	public void updateUser(UsrUser user, String[] splitArrayHtml) {
 		userDao.update(user);
 		
 		delAllUserRole(user.getUserid());
 		
 		for(String str: splitArrayHtml){
 			try {
				updateUserRole(user.getUserid(), Long.valueOf(str));
			} catch (NumberFormatException e) {}
 				catch (Exceptions e) {}
 		} 			
	}
	
	private void delAllUserRole(long userid){
		String hql= "UsrUserRole userRole where userRole.usrUser.userid="+userid; 
		List<?> list=userDao.findQuery("userRole.id",hql);
 	 	if(list!=null){
 	 		int listsize=list.size();
			for(int i=0;i<listsize;i++){
	 	 		UsrUserRole temp=userRoleDao.findOne("id", Integer.parseInt(list.get(i).toString()));
	 	 		userRoleDao.delete(temp);
	 	 	}
		}
	}
	
 	private void updateUserRole(long userid,long roleid) throws Exceptions{
 		UsrUserRole a=new UsrUserRole();
 		UsrUser user=userDao.findOne("userid",userid); 		
 		UsrRole role=roleDao.findOne("roleid",roleid); 		

 		a.setEnabled(true);
 		a.setCreatetime(Time.CurrentTimestamp());
 		a.setUsrRole(role);
 		a.setUsrUser(user);
 		
 		userRoleDao.save(a);
 	}
 	
 	public Long getidByname(String name){
 		return userDao.findOne("useraccount",name).getUserid();
 	}
 	
 	public void delete_user(long userid){
 		//先找到用户角色关系表
 		String hql= "UsrUserRole userRole " +
 				" where userRole.usrUser.userid="+userid;
 		List<?> list=userDao.findQuery("userRole.id ",hql);
 		int id=Integer.parseInt(list.get(0).toString());
 		UsrUserRole a=userRoleDao.findOne("id",id);
 		//删除用户角色关系表
 		userRoleDao.delete(a);
 		
 		//先找到用户
 		UsrUser user=userDao.findOne("userid",userid);
 		//删除用户
 		userDao.delete(user);
 	}
 	
 	public List get_allrole(){
 		return roleDao.findAll();
 	}
 	
 	public List<?> get_this_roleid(long userid) throws Exceptions{
 		String hql= "UsrUserRole userRole " +
 				" where userRole.usrUser.userid="+userid;
 		return  userDao.findQuery("userRole.usrRole.roleid ",hql);
 		//List list=userDao.findQuery("userRole.usrRole.roleid ",hql);
 		//if(list==null) throw new Exceptions("用户与角色关系查询异常，可能是数据库没有此记录！");
 		//return list.get(0).toString();
 	}
 	
 	//------------------role----------------
 	public List get_rolelist(){
 		return roleDao.findModel("ALL",10);
 	}

 	public UsrRole get_role(int roleid){
 		return roleDao.findOne("roleid", roleid);
 	}
 	
 	public void updateRole(UsrRole role){
 		roleDao.update(role);
 	}
 	
 	//根据roleid删除userRole表中的记录
 	private void deleteUserroleByRoleid(long roleid){
		String hql= "UsrUserRole userRole where userRole.usrRole.roleid="+roleid; 
		List<?> list=userRoleDao.findQuery("userRole.id",hql);
 	 	if(list!=null){
 	 		int listsize=list.size();
			for(int i=0;i<listsize;i++){
	 	 		UsrUserRole temp=userRoleDao.findByOne("id="+list.get(i).toString());
	 	 		userRoleDao.delete(temp);
	 	 	}
		}
 	}
 	
 	//根据roleid删除roleAuth表中的记录
 	private void deleteRoleauthByRoleid(long roleid){
		String hql= "UsrRoleAuth roleAuth where roleAuth.usrRole.roleid="+roleid; 
		List<?> list=roleAuthDao.findQuery("roleAuth.id",hql);
 	 	if(list!=null){
 	 		int listsize=list.size();
			for(int i=0;i<listsize;i++){
	 	 		UsrRoleAuth temp=roleAuthDao.findByOne("id="+list.get(i).toString());
	 	 		roleAuthDao.delete(temp);
	 	 	}
		}
 	}
 	
 	
 	public void delete_role(long roleid){
 		deleteUserroleByRoleid(roleid);
 		deleteRoleauthByRoleid(roleid);

 		UsrRole role=roleDao.findOne("roleid",roleid);
 		roleDao.delete(role);
 	}
 	
 	public List<String> get_all_role_name(){
 		List<String> list=new ArrayList<String>();
 		List<UsrRole> listdao=roleDao.findAll();
 		int size=listdao.size();
 		for(int i=0;i<size;i++)
 			list.add(listdao.get(i).getRolename());
 		return list;
 	}
 	
 	public List get_role_in_auth(){
 		return authDao.findAll();
 	}
 	
 	@SuppressWarnings({ "rawtypes"})
	public List get_role_in_auth(int id,boolean isExist){
 		String  hql=null;
 		if(isExist){
 			hql= " usr_role_auth roleAuth , usr_auth auth" +
 				" where roleAuth.roleid="+id+" and roleAuth.authid=auth.authid ";
 		}else{
 			hql= "usr_auth auth" +
 	 				" where not exists ("+ 					
 	 				"select roleAuth.authid from usr_role_auth roleAuth " +
 	 				" where roleAuth.roleid="+id+" and roleAuth.authid=auth.authid )";
 		}	
 		
 		return roleDao.findSqlQuery("auth.authid",hql,0);
 	}
 	
 	public List get_this_authid(int roleid){
 		String hql= "UsrRoleAuth roleAuth " +
 				" where roleAuth.usrRole.roleid="+roleid;
 		return  userDao.findQuery("roleAuth.usrAuth.authid ",hql);
 	}
 	
 	public void updateRoleAuthByAdd(int roleid,String[] add){
 		//填加之前先删除所有的
 		deleteRoleauthByRoleid(roleid);
 		
 		UsrRole role=roleDao.findOne("roleid",roleid);

 		for(String thisid : add){
	 		int temp=Integer.parseInt(thisid);
	 		UsrRoleAuth roleAuth = new UsrRoleAuth();
	 		
	 		UsrAuth auth=authDao.findOne("authid",temp);
	 	
	 		roleAuth.setUsrRole(role);
	 		roleAuth.setUsrAuth(auth);
	 		roleAuthDao.save(roleAuth);
	 	}
		
 	}
 	
 	public void updateRoleAuthByDel(int roleid,String[] del){
 		for(String thisid : del){
 			int temp=Integer.parseInt(thisid);
 			UsrRoleAuth roleAuthd = new UsrRoleAuth();
 			
 			roleAuthd=roleAuthDao.findOne("authid",temp);
 			roleAuthDao.delete(roleAuthd);
 		}
 	}
 	
 	//-----------------res--------------------
 	public List get_reslist(){
 		return resDao.findModel("ALL",10);
 	}

 	public UsrRes get_res(int resid){
 		return resDao.findOne("resid", resid);
 	}
 	
 	public void updateRes(UsrRes res){
 		resDao.update(res);
 	}
 	
 	public void delete_res(long resid){
 		UsrRes res=resDao.findOne("resid",resid);
 		resDao.delete(res);
 	}
 	
 	//---------------------auth----------------------
 	public List get_authlist(){
 		return authDao.findModel("ALL",10);
 	}

 	public UsrAuth get_auth(int authid){
 		return authDao.findOne("authid", authid);
 	}
 	
 	public void updateAuth(UsrAuth auth){
 		authDao.update(auth);
 	}
 	
 	public void delete_auth(long authid){
 		UsrAuth auth=authDao.findOne("authid",authid);
 		authDao.delete(auth);
 	}

 	public List<String> get_all_auth_name(){
 		List<String> list=new ArrayList<String>();
 		List<UsrAuth> listdao=authDao.findAll();
 		for(int i=0;i<listdao.size();i++){
 			list.add(listdao.get(i).getAuthname());
 			p(listdao.get(i).getAuthname());
 		}
 		return list;
 	}
 	
 	
 	
 	//-------------------------------------------
	public UsrUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UsrUserDao userDao) {
		this.userDao = userDao;
	}

	public UsrRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(UsrRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public UsrResDao getResDao() {
		return resDao;
	}

	public void setResDao(UsrResDao resDao) {
		this.resDao = resDao;
	}

	public UsrAuthDao getAuthDao() {
		return authDao;
	}

	public void setAuthDao(UsrAuthDao authDao) {
		this.authDao = authDao;
	}

	public UsrUserRoleDao getUserRoleDao() {
		return userRoleDao;
	}

	public void setUserRoleDao(UsrUserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	public UsrRoleAuthDao getRoleAuthDao() {
		return roleAuthDao;
	}

	public void setRoleAuthDao(UsrRoleAuthDao roleAuthDao) {
		this.roleAuthDao = roleAuthDao;
	}


}
